package com.yadong.smarthome2.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yadong.smarthome2.device.Device;
import com.yadong.smarthome2.device.Device.DeviceState;

public class DeviceListAdapter extends BaseAdapter {
    private LayoutInflater mLayoutInflater; 

    public DeviceListAdapter(Context context) {
        mLayoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    private List<Device> mDevices = new ArrayList<Device>();

    public void setDevices(List<Device> devices) {
        if (devices == null) {
            mDevices.clear();
        } else {
            mDevices = devices;
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDevices.size();
    }

    @Override
    public Object getItem(int position) {
        return mDevices.get(position);
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        if (view == null) {
            
            view = mLayoutInflater.inflate(
                    android.R.layout.simple_list_item_1, viewGroup, false);
        }
        Device device = mDevices.get(position);
        TextView textView = ((TextView) view.findViewById(android.R.id.text1));
        textView.setText(
                device.getDeviceConnectionType() + ":" + device.getDeviceDescription());
        if (device.getState().ordinal() >= DeviceState.CONNECTED.ordinal()) {
            textView.setTextColor(Color.GREEN);
        } else {
            textView.setTextColor(Color.RED);
        }
        return view;
    }
}